package com.project.forlearning.service.impl;

import com.alibaba.fastjson.JSON;
import com.project.forlearning.constant.ResponseTool;
import com.project.forlearning.dao.BackResourceMapper;
import com.project.forlearning.pojo.Resource;
import com.project.forlearning.pojo.vo.ResponseInfo;
import com.project.forlearning.service.BackResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

@Service
public class BackResourceServiceImpl implements BackResourceService {

    @Autowired
    private BackResourceMapper backResourceMapper;

    /**
     * 查询资源
     */
    @Override
    public String findResource(Integer resourceId, String status) {
        List<Resource> resourceList = backResourceMapper.findResource(resourceId, status);

        //若查询结果为空
        if(resourceList == null)
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.QUERY_SUCCESS_ALL.code, ResponseTool.QUERY_SUCCESS_ALL.msg, new ArrayList<>()));
        return JSON.toJSONString(new ResponseInfo<>(ResponseTool.QUERY_SUCCESS.code, ResponseTool.QUERY_SUCCESS.msg, resourceList));
    }

    /**
     * 删除资源
     */
    @Override
    public String deleteResource(Integer resourceId, String status) {

        //删除成功
        if(backResourceMapper.deleteResource(resourceId, status))
                return JSON.toJSONString(new ResponseInfo<>(ResponseTool.DELETE_SUCCESS.code, ResponseTool.DELETE_SUCCESS.msg, true));
        return JSON.toJSONString(new ResponseInfo<>(ResponseTool.DELETE_FAIL.code, ResponseTool.DELETE_FAIL.msg, false));
    }

    /**
     *  增加资源
     */
    @Override
    public String addResource(String title, String introduction, String url, String labels, String icon, String details, int userId, String status) {

        Resource resource = new Resource();
        resource.setTitle(title);
        resource.setIntroduction(introduction);
        resource.setUrl(url);
        resource.setLabels(labels);
        resource.setIcon(icon);
        resource.setDetails(details);
        resource.setUserId(userId);
        resource.setStatus(status);

        if(backResourceMapper.addResource(resource))
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.ADD_SUCCESS.code, ResponseTool.ADD_SUCCESS.msg, resource));
        return JSON.toJSONString(new ResponseInfo<>(ResponseTool.ADD_FAIL.code, ResponseTool.ADD_FAIL.msg, false));
    }

    /**
     * 修改资源
     */
    @Override
    public String updateResource(int resourceId, String title, String introduction, String url, String labels, String icon, String details, Integer collectionNum, Integer scanNum, Integer starNum, String status) {
        Resource resource = new Resource();
        resource.setResourceId(resourceId);
        resource.setIntroduction(introduction);
        resource.setTitle(title);
        resource.setUrl(url);
        resource.setIcon(icon);
        resource.setLabels(labels);
        resource.setDetails(details);
        resource.setCollectionNum(collectionNum);
        resource.setScanNum(scanNum);
        resource.setStarNum(starNum);
        resource.setStatus(status);
        if(backResourceMapper.updateResource(resource))
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.UPDATE_SUCCESS.code, ResponseTool.UPDATE_SUCCESS.msg, resource));
        return JSON.toJSONString(new ResponseInfo<>(ResponseTool.UPDATE_FAIL.code, ResponseTool.UPDATE_FAIL.msg, false));
    }

}
